TWB.New_Module({
	///////////////////////////
	name : "Scraper",          //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	Main : function(){
		if(!TWB.logged()){
			return;
		}
		var vil_id=TWB.Modules.BackNext.GetVil();
		var WN=TWB._window();
		if(typeof WN.wrappedJSObject!="undefined"){
			WN=WN.wrappedJSObject;
		}
		
		// Always make sure to have a session
		if(typeof TWB._SESSION=="undefined"){
			TWB._SESSION={};
		}
		
		// Update builds
		if(typeof TWB._SESSION.BUILDS=="undefined"){
			TWB._SESSION.BUILDS={};
		}
		if(typeof TWB._SESSION.BUILDS[vil_id]=="undefined"){
			TWB._SESSION.BUILDS[vil_id]={};
		}
		WN=WN.game_data.village.buildings;
		var build;
		for(build in WN){
			TWB._SESSION.BUILDS[vil_id][build]=WN[build];
		}
		TWB._SESSION.BUILDS[vil_id].time=TWB.Time();
		
		switch(TWB.Modules.Icons.Screen()){
			case "smith": 
				if(typeof TWB._SESSION.TECHS=="undefined"){
					TWB._SESSION.TECHS={};
				}
				if(typeof TWB._SESSION.TECHS[vil_id]=="undefined"){
					TWB._SESSION.TECHS[vil_id]={};
				}
				var i;
				var UN=TWB.Xpath("//a[contains(@href,'popup_unit')]",6);
				for(i=0; i<UN.length; i++){
					var unit=UN[i].href.match(/unit=(\w+)/)[1];
					var level=UN[i].href.match(/level=(\d+)/)[1];
					if(typeof level=="undefined"){
						level=0;
					}
					TWB._SESSION.TECHS[vil_id][unit]=level;
				}
				TWB._SESSION.TECHS[vil_id].time=TWB.Time();
			break;
			
			case "place" :
				if(typeof TWB._SESSION.UNITS=="undefined"){
					TWB._SESSION.UNITS={};
				}
				if(typeof TWB._SESSION.UNITS[vil_id]=="undefined"){
					TWB._SESSION.UNITS[vil_id]={};
				}
				var i;
				var UN=TWB.Xpath("//a[contains(@href,'#unit_input')]",6);
				for(i=0; i<UN.length; i++){
					var unit=UN[i].href.match(/unit_input_(\w+)/)[1];
					var count=UN[i].href.match(/20(\d+)/)[1];
					TWB._SESSION.UNITS[vil_id][unit]=count;
				}
				TWB._SESSION.UNITS[vil_id].time=TWB.Time();
			break;
			
			case "report" :
				if(TWB.URL().match(/view=(\d+)/)){
					if(typeof TWB._SESSION.REPORTS=="undefined"){
						TWB._SESSION.REPORTS=[];
					}
					
					var i,j;
					var report={};
					
					/* Report Id */
					report.id=TWB.URL().match(/view=(\d+)/)[1];
					
					for(i=0; i<TWB._SESSION.REPORTS.length; i++){
						if(TWB._SESSION.REPORTS[i].id==report.id){
							// Each report is scraped once
							return;
						}
					}
					
					try{
						try{
							/* Report Info */
							report.time=TWB.Time();
							report.name=TWB.Xpath("//*[@id='label']/span",9).textContent;
							report.date=TWB.Xpath("//td[@class='nopad']/table[2]/tbody/tr[2]/td[2]",9).textContent;
							report.luck=TWB.Xpath("//table[@id='attack_luck']//tr/td",9).textContent;
							report.morale=TWB.Xpath("//table[@id='attack_luck']",9).nextSibling.nextSibling.textContent.match(/\d+/)[0];
						}catch(e){
							// This is not an attack report
							return;
						}
						
						/* Attacker */
						report.attackerid=TWB.Xpath("//*[@id='attack_info_att']//a[contains(@href,'info_player')]",9);
						if(typeof report.attackerid=="undefined"){
							report.attackerid=0;
							report.attacker="-";
						}
						else{
							report.attackerid=report.attackerid.href.match(/id=(\d+)/)[1];
							report.attacker=TWB.Xpath("//*[@id='attack_info_att']//a[contains(@href,'info_player')]",9).textContent;
						}
						/* Attacker Vil */
						report.attackervilid=TWB.Xpath("//*[@id='attack_info_att']//a[contains(@href,'info_vil')]",9).href.match(/id=(\d+)/)[1];
						report.attackervil=TWB.Xpath("//*[@id='attack_info_att']//a[contains(@href,'info_vil')]",9).textContent;
						/* Attacker Units */
						var IMG=TWB.Xpath("//*[@id='attack_info_att_units']//tr[1]//img",6);
						var ROW1=TWB.Xpath("//*[@id='attack_info_att_units']//tr[2]/td",6);
						var ROW2=TWB.Xpath("//*[@id='attack_info_att_units']//tr[3]/td",6);
						report.attackerunits=[{},{}];
						for(j=0; j<IMG.length; j++){
							report.attackerunits[0][IMG[j].src.match(/unit_(\w+)/)[1]]=ROW1[j+1].textContent;
							report.attackerunits[1][IMG[j].src.match(/unit_(\w+)/)[1]]=ROW2[j+1].textContent
						}
						
						/* Defender */
						report.defenderid=TWB.Xpath("//*[@id='attack_info_def']//a[contains(@href,'info_player')]",9);
						if(typeof report.defenderid=="undefined"){
							report.defenderid=0;
							report.defender="-";
						}
						else{
							report.defenderid=report.defenderid.href.match(/id=(\d+)/)[1];
							report.defender=TWB.Xpath("//*[@id='attack_info_def']//a[contains(@href,'info_player')]",9).textContent;
						}
						/* Defender Vil */
						report.defendervilid=TWB.Xpath("//*[@id='attack_info_def']//a[contains(@href,'info_vil')]",9).href.match(/id=(\d+)/)[1];
						report.defendervil=TWB.Xpath("//*[@id='attack_info_def']//a[contains(@href,'info_vil')]",9).textContent;
						/* Defender Units */
						var IMG=TWB.Xpath("//*[@id='attack_info_def_units']//tr[1]//img",6);
						var ROW1=TWB.Xpath("//*[@id='attack_info_def_units']//tr[2]/td",6);
						var ROW2=TWB.Xpath("//*[@id='attack_info_def_units']//tr[3]/td",6);
						report.defenderunits=[{},{}];
						for(j=0; j<IMG.length; j++){
							report.defenderunits[0][IMG[j].src.match(/unit_(\w+)/)[1]]=ROW1[j+1].textContent;
							report.defenderunits[1][IMG[j].src.match(/unit_(\w+)/)[1]]=ROW2[j+1].textContent
						}
						
						/* Push / Update */
						TWB._SESSION.REPORTS.push(report);
					}catch(e){
						TWB.log(e,"Scraper");
					}
				}
			break;
			
			case "overview_villages" : 
				switch(TWB.Modules.Icons.Mode()){
					case "groups" :
						if(typeof TWB._SESSION.GROUPS=="undefined"){
							TWB._SESSION.GROUPS={};
						}
						
						var group=TWB.URL().match(/group=(\d+)/);
						if(group!==null && typeof group[1]!="undefined"){
							group=group[1];
							
							// Rebuild
							TWB._SESSION.GROUPS[group]=[];
							var i;
							var entries=TWB.Xpath("//input[@name='village_ids[]']",6);
							for(i=0; i<entries.length; i++){
								TWB._SESSION.GROUPS[group].push(entries[i].getAttribute("value"));
							}
						}
					break;
				}
				if(TWB.get_var("autolvil")){
					TWB.Modules.Villages.Update();
				}
			break;
		}
	},
	priority : 22,
	init : function(){
		TWB.Processor.start("scraper","Modules.Scraper.Main","*",false);
	}
});
